package com.ks.util;

import org.apache.commons.lang3.StringUtils;

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @author : LiuH
 * @date : 2019/5/29 14:13
 * @Version 1.0
 */
public class StringUtil {
    /**
     * 判断是否为数字
     * @param str
     * @return
     */
    public static boolean isNumeric(String str){

        for (int i = 0; i < str.length(); i++){

            if (!Character.isDigit(str.charAt(i))){
                return false;
            }

        }
        return true;
    }

    /**
     * 字符串转为区间数组
     * @param str
     * @param regex
     * @param count
     * @return
     */
    public static int[] strToIntArray(String str,String regex,int count){
        if(StringUtils.isEmpty(str) || StringUtils.isEmpty(regex)) return null;
        String[] array = str.split(regex);
        if(array.length!=count)return null;
        int[] res = new int[count];
        for (int i = 0; i < count; i++) {
            res[i] = Integer.valueOf(array[i]);
        }
        return res;
    }

    public static boolean isEmpty(String str) {
        return str == null || str.length() == 0;
    }

    public static String encodeURIComponent(String str) {
        if (str == null)
            return "";
        try {
            return URLEncoder.encode(str.replace("\"", ""), "UTF-8").replace("+", "%20");
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
            return "";
        }
    }

    public static String decodeURIComponent(String str) {
        if (str == null)
            return "";
        try {
            return URLDecoder.decode(str, "UTF-8");
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
            return "";
        }
    }

    public static List<String> explode(String delimiter, String str) {
        if(StringUtils.isBlank(str))return null;
        String parts[] = str.split(delimiter);
        List<String> arr = new ArrayList<String>();
        for (int i = 0; i < parts.length; i++)
            arr.add(parts[i].trim());
        return arr;
    }

    public static List<Integer> explodeInt(String delimiter, String str) {
        String parts[] = str.split(delimiter);
        List<Integer> arr = new ArrayList<Integer>();
        for (int i = 0; i < parts.length; i++)
            arr.add(Integer.valueOf(parts[i].trim()));
        return arr;
    }

    public static String implodeInt(String delimiter, List<Integer> arr) {
        if (arr == null)
            return "";
        boolean first = true;
        StringBuilder sb = new StringBuilder();

        for (Integer e : arr) {
            if (!first)
                sb.append(delimiter);
            sb.append(e.toString());
            first = false;
        }

        return sb.toString();
    }

    public static String implodeMapSI(String delimiter, Map<String, Integer> map) {
        if (map == null)
            return "";
        boolean first = true;
        StringBuilder sb = new StringBuilder();

        Iterator<Map.Entry<String, Integer>> entries = map.entrySet().iterator();
        while (entries.hasNext()) {
            Map.Entry<String, Integer> entry = entries.next();
            if (!first)
                sb.append(delimiter);
            sb.append(String.format("{'%s':%d}", entry.getKey(), entry.getValue()));
            first = false;
        }

        return sb.toString();
    }

    public static String implodeMapSList(String delimiter, Map<String, List<String>> map) {
        if (map == null)
            return "";
        boolean first = true;
        StringBuilder sb = new StringBuilder();

        Iterator<Map.Entry<String,  List<String>>> entries = map.entrySet().iterator();
        while (entries.hasNext()) {
            Map.Entry<String,  List<String>> entry = entries.next();
            if (!first)
                sb.append(delimiter);
            sb.append(String.format("{'%s':[%s]}", entry.getKey(), StringUtil.implode(",", entry.getValue())));
            first = false;
        }

        return sb.toString();
    }

    public static String implodeMapSS(String delimiter, Map<String, String> map) {
        if (map == null)
            return "";
        boolean first = true;
        StringBuilder sb = new StringBuilder();

        Iterator<Map.Entry<String, String>> entries = map.entrySet().iterator();
        while (entries.hasNext()) {
            Map.Entry<String, String> entry = entries.next();
            if (!first)
                sb.append(delimiter);
            sb.append(String.format("{'%s':%s}", entry.getKey(), entry.getValue()));
            first = false;
        }

        return sb.toString();
    }

    public static String implodeInt(String delimiter, int[] arr, int n) {
        if (arr == null)
            return "";
        StringBuilder sb = new StringBuilder();

        for (int i = 0; i < n; i++) {
            if (i > 0)
                sb.append(delimiter);
            sb.append(Integer.toString(arr[i]));
        }

        return sb.toString();
    }

    public static String implodeInt(String delimiter, int[] arr) {
        if (arr == null)
            return "";
        StringBuilder sb = new StringBuilder();

        for (int i = 0; i < arr.length; i++) {
            if (i > 0)
                sb.append(delimiter);
            sb.append(Integer.toString(arr[i]));
        }

        return sb.toString();
    }

    public static String implode(String delimiter, List<String> arr) {
        if (arr == null)
            return "";
        boolean first = true;
        StringBuilder sb = new StringBuilder();

        for (String e : arr) {
            if (!first)
                sb.append(delimiter);
            sb.append(e);
            first = false;
        }

        return sb.toString();
    }

    public static String[] extractNumberMsg(String ptCasinoMsg){
        String returnAmounts [] = new String [10];
        ptCasinoMsg = ptCasinoMsg.replace(" | ", " ");
        String [] amounts = ptCasinoMsg.split(" ");
        for(int i=0;i<amounts.length;i++){
            Pattern p= Pattern.compile("(\\d+\\.\\d+)");
            Matcher m=p.matcher(amounts[i]);
            if(m.find()){
                returnAmounts[i]=m.group(1);
            }else{
                p= Pattern.compile("(\\d+)");
                m=p.matcher(amounts[i]);
                if(m.find()){
                    returnAmounts[i]=m.group(1);
                }
            }
        }
        return returnAmounts;
    }
    public static int getNumberFromMsg(String ptCasinoMsg){
        String[] arr = extractNumberMsg(ptCasinoMsg);
        return null == arr || arr.length == 0?0:Integer.valueOf(arr[0]);
    }
    public static void main(String[] args){
//    String str ="2月9日请假条";
//        String[] arr = extractNumberMsg(str);
//        System.out.println(arr[0].toString());

        printNum(1741,180);
        printNum(175,18);
    }

    public static void printNum(int startNum,int count){
        StringBuilder builder = new StringBuilder();
        for (int i = startNum; i < startNum+count; i++) {
            builder.append(i);
            builder.append(",");
        }
        System.out.println(builder.toString());
    }
}
